package com.sn.pw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sn.pw.code.impl.BaseErrorCode;
import com.sn.pw.dao.entity.User;
import com.sn.pw.dao.mapper.UserMapper;
import com.sn.pw.exception.ServiceException;
import com.sn.pw.page.ConventPage;
import com.sn.pw.page.PageReq;
import com.sn.pw.page.PageResp;
import com.sn.pw.result.Result;
import com.sn.pw.service.UserService;
import com.sn.pw.vo.createreq.UserCreateReq;
import com.sn.pw.vo.req.LoginReq;
import com.sn.pw.vo.resp.UserResp;
import com.sn.pw.vo.updatereq.UserUpdateReq;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sn.pw.code.impl.BaseErrorCode.SERVICE_ERROR;
import static com.sn.pw.code.impl.BaseErrorCode.USER_NAME_VERIFY_ERROR;

/**
 * <p>
 * 人员信息 服务实现类
 * </p>
 *
 * @author mx
 * @since 2023-12-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(UserCreateReq param) {
        //检验手机号是否已经存在
        LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery().eq(User::getPhone, param.getPhone());
        User user = getOne(lambdaQueryWrapper);
        if (ObjectUtil.isNotEmpty(user)){
            throw new ServiceException(BaseErrorCode.USER_ERROR);
        }
        if (ObjectUtil.isEmpty(param.getPersonName())) {
            throw new ServiceException(BaseErrorCode.USERNAME_ERROR);
        }
        if (ObjectUtil.isEmpty(param.getPhone())) {
            throw new ServiceException(BaseErrorCode.PHONE_ERROR);
        }
        if (ObjectUtil.isEmpty(param.getPassword())) {
            param.setPassword("123456");
        }
        return super.save(BeanUtil.toBean(param, User.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(List<Long> ids) {
        return super.removeBatchByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(UserUpdateReq param) {
        return super.updateById(BeanUtil.toBean(param, User.class));
    }

    @Override
    public UserResp findById(Long id) {
        User info = super.getById(id);
        if (ObjectUtil.isEmpty(info)) {
            throw new ServiceException(SERVICE_ERROR);
        }
        return BeanUtil.toBean(info, UserResp.class);
    }

    @Override
    public List<UserResp> findList(List<Long> ids) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery().in(User::getId, ids);
        List<User> list = super.list(lambdaQueryWrapper);
        return BeanUtil.copyToList(list, UserResp.class);
    }

    @Override
    public PageResp<UserResp> findPage(PageReq pageReq) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
        IPage<User> page = super.page(ConventPage.getPage(pageReq), lambdaQueryWrapper);
        return ConventPage.getPageResp(page, UserResp.class);
    }

    @Override
    public Result userLogin(LoginReq param) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery().eq(User::getPhone, param.getUserName());
        User user = getOne(lambdaQueryWrapper);
        //验证用户是否存在
        if (ObjectUtil.isEmpty(user)) {
            throw new ServiceException(USER_NAME_VERIFY_ERROR);
        }
        //验证账号
        if (ObjectUtil.isEmpty(param.getUserName())) {
            throw new ServiceException(BaseErrorCode.USERNAME_ERROR);
        }
        if (ObjectUtil.isEmpty(param.getPassword())) {
            throw new ServiceException(BaseErrorCode.PASSWORD_ERROR);
        }
        if (!user.getPhone().equals(param.getUserName()) || !user.getPassword().equals(param.getPassword())) {
            throw new ServiceException(BaseErrorCode.LOGIN_ERROR);
        }

        return Result.success("正常登录成功");
    }


}
